/*     */ package com.zimbra.cs.index;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.db.DbSearch.FetchMode;
/*     */ import com.zimbra.cs.db.DbSearch.Result;
/*     */ import com.zimbra.cs.imap.ImapMessage;
/*     */ import com.zimbra.cs.mailbox.CalendarItem;
/*     */ import com.zimbra.cs.mailbox.Contact;
/*     */ import com.zimbra.cs.mailbox.MailItem;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.Message;
/*     */ import com.zimbra.cs.mailbox.Note;
/*     */ import com.zimbra.cs.mailbox.Task;
/*     */ import java.util.LinkedHashMap;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ abstract class ZimbraQueryResultsImpl
/*     */   implements ZimbraQueryResults
/*     */ {
/*     */   private static final int MAX_LRU_ENTRIES = 2048;
/*     */   private static final int INITIAL_TABLE_SIZE = 100;
/*     */   private Map<Integer, ConversationHit> conversationHits;
/*     */   private Map<Integer, MessageHit> messageHits;
/*     */   private Map<String, MessagePartHit> partHits;
/*     */   private Map<Integer, ContactHit> contactHits;
/*     */   private Map<Integer, NoteHit> noteHits;
/*     */   private Map<Integer, CalendarItemHit> calItemHits;
/*     */   private final Set<MailItem.Type> types;
/*     */   private final SortBy sortBy;
/*     */   private final SearchParams.Fetch fetch;
/*     */   
/*     */   static final class LRUHashMap<T, U>
/*     */     extends LinkedHashMap<T, U>
/*     */   {
/*     */     private static final long serialVersionUID = -6181398012977532525L;
/*     */     private final int max;
/*     */     
/*     */     LRUHashMap(int max)
/*     */     {
/*  48 */       super(0.75F, true);
/*  49 */       this.max = max;
/*     */     }
/*     */     
/*     */     LRUHashMap(int max, int size) {
/*  53 */       super(0.75F, true);
/*  54 */       this.max = max;
/*     */     }
/*     */     
/*     */     protected boolean removeEldestEntry(Map.Entry<T, U> eldest)
/*     */     {
/*  59 */       return size() > this.max;
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   ZimbraQueryResultsImpl(Set<MailItem.Type> types, SortBy sort, SearchParams.Fetch fetch)
/*     */   {
/*  78 */     this.types = types;
/*  79 */     this.fetch = fetch;
/*  80 */     this.sortBy = sort;
/*     */     
/*  82 */     this.conversationHits = new LRUHashMap(2048, 100);
/*  83 */     this.messageHits = new LRUHashMap(2048, 100);
/*  84 */     this.partHits = new LRUHashMap(2048, 100);
/*  85 */     this.contactHits = new LRUHashMap(2048, 100);
/*  86 */     this.noteHits = new LRUHashMap(2048, 100);
/*  87 */     this.calItemHits = new LRUHashMap(2048, 100);
/*     */   }
/*     */   
/*     */   public abstract ZimbraHit skipToHit(int paramInt)
/*     */     throws ServiceException;
/*     */   
/*     */   public boolean hasNext() throws ServiceException
/*     */   {
/*  95 */     return peekNext() != null;
/*     */   }
/*     */   
/*     */   public SortBy getSortBy()
/*     */   {
/* 100 */     return this.sortBy;
/*     */   }
/*     */   
/*     */   Set<MailItem.Type> getTypes() {
/* 104 */     return this.types;
/*     */   }
/*     */   
/*     */   public SearchParams.Fetch getFetchMode() {
/* 108 */     return this.fetch;
/*     */   }
/*     */   
/*     */   protected ConversationHit getConversationHit(Mailbox mbx, int id, Object sortValue) {
/* 112 */     ConversationHit hit = (ConversationHit)this.conversationHits.get(Integer.valueOf(id));
/* 113 */     if (hit == null) {
/* 114 */       hit = new ConversationHit(this, mbx, id, sortValue);
/* 115 */       this.conversationHits.put(Integer.valueOf(id), hit);
/*     */     }
/* 117 */     return hit;
/*     */   }
/*     */   
/*     */   protected ContactHit getContactHit(Mailbox mbx, int id, Contact contact, Object sortValue) {
/* 121 */     ContactHit hit = (ContactHit)this.contactHits.get(Integer.valueOf(id));
/* 122 */     if (hit == null) {
/* 123 */       hit = new ContactHit(this, mbx, id, contact, sortValue);
/* 124 */       this.contactHits.put(Integer.valueOf(id), hit);
/*     */     }
/* 126 */     return hit;
/*     */   }
/*     */   
/*     */   protected NoteHit getNoteHit(Mailbox mbx, int id, Note note, Object sortValue) {
/* 130 */     NoteHit hit = (NoteHit)this.noteHits.get(Integer.valueOf(id));
/* 131 */     if (hit == null) {
/* 132 */       hit = new NoteHit(this, mbx, id, note, sortValue);
/* 133 */       this.noteHits.put(Integer.valueOf(id), hit);
/*     */     }
/* 135 */     return hit;
/*     */   }
/*     */   
/*     */   protected CalendarItemHit getAppointmentHit(Mailbox mbx, int id, CalendarItem cal, Object sortValue) {
/* 139 */     CalendarItemHit hit = (CalendarItemHit)this.calItemHits.get(Integer.valueOf(id));
/* 140 */     if (hit == null) {
/* 141 */       hit = new CalendarItemHit(this, mbx, id, cal, sortValue);
/* 142 */       this.calItemHits.put(Integer.valueOf(id), hit);
/*     */     }
/* 144 */     return hit;
/*     */   }
/*     */   
/*     */   protected CalendarItemHit getTaskHit(Mailbox mbx, int id, Task task, Object sortValue) {
/* 148 */     CalendarItemHit hit = (CalendarItemHit)this.calItemHits.get(Integer.valueOf(id));
/* 149 */     if (hit == null) {
/* 150 */       hit = new TaskHit(this, mbx, id, task, sortValue);
/* 151 */       this.calItemHits.put(Integer.valueOf(id), hit);
/*     */     }
/* 153 */     return hit;
/*     */   }
/*     */   
/*     */   protected MessageHit getMessageHit(Mailbox mbx, int id, Message msg, org.apache.lucene.document.Document doc, Object sortValue) {
/* 157 */     MessageHit hit = (MessageHit)this.messageHits.get(Integer.valueOf(id));
/* 158 */     if (hit == null) {
/* 159 */       hit = new MessageHit(this, mbx, id, msg, doc, sortValue);
/* 160 */       this.messageHits.put(Integer.valueOf(id), hit);
/*     */     }
/* 162 */     return hit;
/*     */   }
/*     */   
/*     */   protected MessagePartHit getMessagePartHit(Mailbox mbx, int id, Message msg, org.apache.lucene.document.Document doc, Object sortValue) {
/* 166 */     String key = Integer.toString(id) + "-" + doc.get("l.partname");
/* 167 */     MessagePartHit hit = (MessagePartHit)this.partHits.get(key);
/* 168 */     if (hit == null) {
/* 169 */       hit = new MessagePartHit(this, mbx, id, msg, doc, sortValue);
/* 170 */       this.partHits.put(key, hit);
/*     */     }
/* 172 */     return hit;
/*     */   }
/*     */   
/*     */   protected DocumentHit getDocumentHit(Mailbox mbx, int id, com.zimbra.cs.mailbox.Document item, org.apache.lucene.document.Document doc, Object sortValue)
/*     */   {
/* 177 */     return new DocumentHit(this, mbx, id, item, doc, sortValue);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   static final boolean shouldAddDuplicateHits(MailItem.Type type)
/*     */   {
/* 188 */     return (type == MailItem.Type.CHAT) || (type == MailItem.Type.MESSAGE);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   ZimbraHit getZimbraHit(Mailbox mbox, DbSearch.Result sr, org.apache.lucene.document.Document doc, DbSearch.FetchMode fetch)
/*     */   {
/* 198 */     MailItem item = null;
/* 199 */     ImapMessage i4msg = null;
/* 200 */     int modseq = -1;int parentId = 0;
/* 201 */     switch (fetch) {
/*     */     case MAIL_ITEM: 
/* 203 */       item = sr.getItem();
/* 204 */       break;
/*     */     case IMAP_MSG: 
/* 206 */       i4msg = sr.getImapMessage();
/* 207 */       break;
/*     */     case MODSEQ: 
/* 209 */       modseq = sr.getModSeq();
/* 210 */       break;
/*     */     case PARENT: 
/* 212 */       parentId = sr.getParentId();
/*     */     }
/*     */     
/*     */     
/* 216 */     ZimbraHit result = null;
/* 217 */     switch (sr.getType()) {
/*     */     case CHAT: 
/*     */     case MESSAGE: 
/* 220 */       if (doc != null) {
/* 221 */         result = getMessagePartHit(mbox, sr.getId(), (Message)item, doc, sr.getSortValue());
/*     */       } else {
/* 223 */         result = getMessageHit(mbox, sr.getId(), (Message)item, null, sr.getSortValue());
/*     */       }
/* 225 */       break;
/*     */     case CONTACT: 
/* 227 */       result = getContactHit(mbox, sr.getId(), (Contact)item, sr.getSortValue());
/* 228 */       break;
/*     */     case NOTE: 
/* 230 */       result = getNoteHit(mbox, sr.getId(), (Note)item, sr.getSortValue());
/* 231 */       break;
/*     */     case APPOINTMENT: 
/* 233 */       result = getAppointmentHit(mbox, sr.getId(), (CalendarItem)item, sr.getSortValue());
/* 234 */       break;
/*     */     case TASK: 
/* 236 */       result = getTaskHit(mbox, sr.getId(), (Task)item, sr.getSortValue());
/* 237 */       break;
/*     */     case DOCUMENT: 
/*     */     case WIKI: 
/* 240 */       result = getDocumentHit(mbox, sr.getId(), (com.zimbra.cs.mailbox.Document)item, doc, sr.getSortValue());
/* 241 */       break;
/*     */     default: 
/* 243 */       if (!$assertionsDisabled) throw new AssertionError();
/*     */       break;
/*     */     }
/* 246 */     if (i4msg != null) {
/* 247 */       result.cacheImapMessage(i4msg);
/*     */     }
/* 249 */     if (modseq > 0) {
/* 250 */       result.cacheModifiedSequence(modseq);
/*     */     }
/* 252 */     if (parentId != 0) {
/* 253 */       result.cacheParentId(parentId);
/*     */     }
/* 255 */     return result;
/*     */   }
/*     */   
/*     */   public boolean isPreSorted()
/*     */   {
/* 260 */     return false;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/index/ZimbraQueryResultsImpl.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */